package com.snowcattle.game.bootstrap;

import com.snowcattle.game.bootstrap.manager.LocalManager;
import com.snowcattle.game.common.constant.GlobalConstants;
import com.snowcattle.game.common.constant.Loggers;
import com.snowcattle.game.common.constant.ServiceName;
import com.snowcattle.game.common.util.ThreadPool;
import com.snowcattle.game.service.IService;
import com.snowcattle.game.service.net.LocalNetService;
import com.snowcattle.game.service.net.tcp.GameNettyRPCService;
import com.snowcattle.game.service.net.tcp.GameNettyTcpServerService;
import com.snowcattle.game.service.net.udp.GameNettyUdpServerService;
import com.snowcattle.game.thread.ThreadNameFactory;
import org.slf4j.Logger;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * 服务器启动结束服务
 */
public class GamerServerStartFinishedService implements IService {

    private Logger logger = Loggers.serverLogger;

    private LocalNetService localNetService;

    private ThreadPool threadPool;

    public LocalNetService getLocalNetService() {
        return localNetService;
    }

    public void setLocalNetService(LocalNetService localNetService) {
        this.localNetService = localNetService;
    }

    @Override
    public String getId() {
        return ServiceName.GamerServerStartFinishedService;
    }

    @Override
    public void startup() throws Exception {
        int coreSize = 3;
        ThreadNameFactory threadNameFactory = new ThreadNameFactory(GlobalConstants.Thread.START_FINISHED);
        localNetService = LocalManager.getInstance().get(LocalNetService.class);
        threadPool = new ThreadPool(coreSize, coreSize, 0, new ArrayBlockingQueue<>(coreSize), threadNameFactory, new ThreadPoolExecutor.DiscardOldestPolicy());
        threadPool.start();
        threadPool.addTask(() -> {
            GameNettyTcpServerService gameNettyTcpServerService = localNetService.getGameNettyTcpServerService();
            if (gameNettyTcpServerService != null) {
                try {
                    gameNettyTcpServerService.finish();
                } catch (Exception e) {
                    Loggers.errorLogger.error(e.toString(), e);
                }
            }
        });

        threadPool.addTask(() -> {
            GameNettyUdpServerService gameNettyUdpServerService = localNetService.getGameNettyUdpServerService();
            if (gameNettyUdpServerService != null) {
                try {
                    gameNettyUdpServerService.finish();
                } catch (Exception e) {
                    Loggers.errorLogger.error(e.toString(), e);
                }
            }
        });

        threadPool.addTask(() -> {
            GameNettyRPCService gameNettyRPCService = localNetService.getGameNettyRPCService();
            if (gameNettyRPCService != null) {
                try {
                    gameNettyRPCService.finish();
                } catch (Exception e) {
                    Loggers.errorLogger.error(e.toString(), e);
                }
            }
        });

        logger.info("netty server sync start finished");
    }

    @Override
    public void shutdown() throws Exception {
        threadPool.stop();
    }
}
